package com.bonzaster;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.*;

/**
 * Created by IntelliJ IDEA.
 * User: Damir
 * Date: 21.04.2010
 * Time: 17:15:23
 */
public class Settings extends Activity
{
    private static SeekBar mFrequencyMinutesSeekBar;
    private static TextView mFrequencyMinutesTitleTextView;
    private static String mFrequencyMinutesTitleString;
    private static SeekBar mFrequencySecondsSeekBar;
    private static TextView mFrequencySecondsTitleTextView;
    private static String mFrequencySecondsTitleString;
    private static EditText mDestinationDirectoryEditText;
    private static EditText mFileNamePrefixEditText;

    private static final int MIN_DELAY = 1;

    private static SharedPreferences mPreferences;
    private static int mDelayMinutes;
    private static int mDelaySeconds;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mPreferences = getSharedPreferences(getResources().getString(R.string.SETTINGS_KEY), MODE_PRIVATE);
        setContentView(R.layout.settings);

        mFrequencySecondsSeekBar = (SeekBar)findViewById(R.id.mFrequencySecondsSeekBar);
        mFrequencySecondsSeekBar.setMax(59);
        mFrequencySecondsSeekBar.setOnSeekBarChangeListener(onSecondsSeekBarChangeListener);

        mFrequencySecondsTitleTextView = (TextView)findViewById(R.id.mFrequencySecondsTitle);
        mFrequencySecondsTitleString = getResources().getString(R.string.SETTINGS_FREQUENCY_SECONDS_TITLE);

        mFrequencyMinutesSeekBar = (SeekBar)findViewById(R.id.mFrequencyMinutesSeekBar);
        mFrequencyMinutesSeekBar.setMax(60);
        mFrequencyMinutesSeekBar.setOnSeekBarChangeListener(onMinutesSeekBarChangeListener);

        mFrequencyMinutesTitleTextView = (TextView)findViewById(R.id.mFrequencyMinutesTitle);
        mFrequencyMinutesTitleString = getResources().getString(R.string.SETTINGS_FREQUENCY_MINUTES_TITLE);

        mDestinationDirectoryEditText = (EditText)findViewById(R.id.mDestinationDirectoryEditText);
        mFileNamePrefixEditText = (EditText)findViewById(R.id.mFileNamePrefixEditText);
    }

    private SeekBar.OnSeekBarChangeListener onSecondsSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener()
    {

        public void onProgressChanged(SeekBar seekBar, int i, boolean b)
        {
            mDelaySeconds = MIN_DELAY + seekBar.getProgress();
            setSecondsTitle(mDelaySeconds);
        }

        public void onStartTrackingTouch(SeekBar seekBar){}

        public void onStopTrackingTouch(SeekBar seekBar){}
    };

    private SeekBar.OnSeekBarChangeListener onMinutesSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener()
    {

        public void onProgressChanged(SeekBar seekBar, int i, boolean b)
        {
            mDelayMinutes = seekBar.getProgress();
            setMinutesTitle(mDelayMinutes);
        }

        public void onStartTrackingTouch(SeekBar seekBar){}

        public void onStopTrackingTouch(SeekBar seekBar){}
    };

    public void onResume()
    {
        super.onResume();

        mDelayMinutes = mPreferences.getInt(getResources().getString(R.string.SETTINGS_FREQUENCY_KEY_MINUTES), 0);
        mDelaySeconds = mPreferences.getInt(getResources().getString(R.string.SETTINGS_FREQUENCY_KEY_SECONDS), 5);

        mFrequencyMinutesSeekBar.setProgress(mDelayMinutes);
        setMinutesTitle(mDelayMinutes);
        mFrequencySecondsSeekBar.setProgress(mDelaySeconds - MIN_DELAY);
        setSecondsTitle(mDelaySeconds);
        mDestinationDirectoryEditText.setText(mPreferences.getString(getResources().getString(R.string.SETTINGS_DESTINATION_DIR_KEY),""));
        mFileNamePrefixEditText.setText(mPreferences.getString(getResources().getString(R.string.SETTINGS_FILE_PREFIX_KEY),""));
    }

    public void onPause()
    {
        SharedPreferences.Editor e = mPreferences.edit();
        e.putInt(getResources().getString(R.string.SETTINGS_FREQUENCY_KEY_MINUTES), mDelayMinutes);
        e.putInt(getResources().getString(R.string.SETTINGS_FREQUENCY_KEY_SECONDS), mDelaySeconds);
        e.putString(getResources().getString(R.string.SETTINGS_DESTINATION_DIR_KEY),
                mDestinationDirectoryEditText.getText().toString());
        e.putString(getResources().getString(R.string.SETTINGS_FILE_PREFIX_KEY),
                mFileNamePrefixEditText.getText().toString());
        e.commit();
        super.onPause();
    }

    private void setSecondsTitle(int seconds)
    {
        mFrequencySecondsTitleTextView.setText(mFrequencySecondsTitleString + " (" + seconds + " s)");
    }

    private void setMinutesTitle(int minutes)
    {
        mFrequencyMinutesTitleTextView.setText(mFrequencyMinutesTitleString + " (" + minutes + " min)");
    }

}
